package com.wu.ssm.mybatis;

import com.wu.ssm.mybatis.binding.MyMapperMethod;
import org.apache.ibatis.mapping.SqlCommandType;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * Created by wzf on 2018/11/11/011.
 */
public class MyMapperProxy <T> implements InvocationHandler, Serializable{

    private MyDefaultSqlsession mySqlsession = null;
    private final Class<T> mapperInterface;

    public MyMapperProxy(MyDefaultSqlsession mySqlsession, Class<T> mapperInterface) {
        this.mySqlsession=mySqlsession;
        this.mapperInterface = mapperInterface;
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//        if(method.getDeclaringClass().getName().equals(UserMapperXml.namespace)){
//            String sql = UserMapperXml.map.get(method.getName());
//            return mySqlsession.selectOne(sql, String.valueOf(args[0]));
//        }

        if(method.getDeclaringClass().getName().equals(UserMapperXml.namespace)){
            String sql = UserMapperXml.map.get(method.getName());
            //return mySqlsession.selectOne(sql,args[0]);

            MyMapperMethod myMapperMethod = new MyMapperMethod(new MyMapperMethod.SqlCommand(method.getName(), SqlCommandType.SELECT));
            return myMapperMethod.execute(mySqlsession,args);

        }
        return null;
    }
}
